const path = require('path'); // 引入path模块
const Response = require('@/utils/response');

class UploadController {
  async upload(ctx) {
    try {
      const {file} = ctx.request.files; // 获取上传的文件
      if (!file) {
        return Response.error(ctx, '请上传文件');
      }
      // const reader = fs.createReadStream(file.path); // 创建读取流
      // const stream = fs.createWriteStream(`./uploads/${file.name}`); // 创建写入流
      // reader.pipe(stream); // 将读取流写入到文件中
      const data = {
        url: `${config.BASE_URL}/local/${path.basename(file.filepath)}`
      }
      return Response.success(ctx, data, '上传成功');
    } catch (error) {
      return Response.error(ctx, '上传失败', 500, error);
    }
  }

}

module.exports = new UploadController();
